import sys
import time

from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout
from concurrent.futures import ThreadPoolExecutor

from mockAction import autoAction

eth_usdt = "https://www.okx.com/demo-trading-explorer/v5/zh/"

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.buy_timer = QTimer(self)
        self.sell_timer = QTimer(self)
        self.buy_num = None
        self.sell_num = None
        self.executor = None  # 初始化时不创建线程池
        self.auto_action = None
    def initUI(self):
        # 设置窗口标题
        self.setWindowTitle('交易自动化')
        self.resize(1000, 600)  # 设置窗口大小

        # 创建垂直布局
        layout = QVBoxLayout(self)

        # 创建标签和输入框，并添加到布局中
        self.entries = {}
        labels_texts = ["chrome驱动地址:", "市场标记:", "买入间隔:", "买入份额:", "卖出间隔:", "卖出份额:"]
        default_values = {
            "chrome驱动地址:": "C:\\Users\\Administrator\\PycharmProjects\\authokxtool\\UD2",
            "市场标记:": "eth-usdt",
            "买入间隔:": "60",  # 以秒为单位
            "买入份额:" : "1",
            "卖出间隔:": "120",  # 以秒为单位
            "卖出份额:":"0.0001"
        }

        for label_text in labels_texts:
            label = QLabel(label_text, self)
            layout.addWidget(label)
            entry = QLineEdit(self)
            entry.setText(default_values[label_text])
            layout.addWidget(entry)
            self.entries[label_text] = entry

        # 创建启动和停止按钮，并添加到布局中
        self.start_button = QPushButton('启动', self)
        self.start_button.clicked.connect(self.start_timers)
        layout.addWidget(self.start_button)

        self.stop_button = QPushButton('停止', self)
        self.stop_button.clicked.connect(self.stop_timers)
        layout.addWidget(self.stop_button)

    def start_buy(self):
        # 提交到线程池的买入操作
        self.executor.submit(self.initiate_buy)

    def start_sell(self):
        # 提交到线程池的卖出操作
        self.executor.submit(self.initiate_sell)


    def initiate_page(self):
        # 获取输入框的内容
        chrome_driver_path = self.entries["chrome驱动地址:"].text()
        self.auto_action = autoAction.AutoAction(chrome_driver_path)

        market_tag = self.entries["市场标记:"].text()
        # 这里执行一次性的任务
        self.auto_action.open_page(autoAction.main_page)
        self.auto_action.login()
        self.auto_action.open_page(eth_usdt + market_tag)
    def initiate_buy(self):
        # 买入操作逻辑，此函数在线程池线程中执行
        self.auto_action.buy(self.buy_num)  # 模拟耗时操作

    def initiate_sell(self):
        # 卖出操作逻辑，此函数在线程池线程中执行
        self.auto_action.sell(self.sell_num)  # 模拟耗时操作

    def start_timers(self):
        if not self.executor:
            self.executor = ThreadPoolExecutor(max_workers=2)  # 在需要时创建线程池

        # 执行启动界面
        self.executor.submit(self.initiate_page)
        time.sleep(10)
        # 配置定时器
        self.buy_timer.timeout.connect(self.start_buy)
        self.sell_timer.timeout.connect(self.start_sell)

        buy_interval = int(self.entries["买入间隔:"].text()) * 1000  # 转换为毫秒
        sell_interval = int(self.entries["卖出间隔:"].text()) * 1000  # 转换为毫秒
        self.buy_num=self.entries["买入份额:"].text()
        self.sell_num=self.entries["卖出份额:"].text()

        # 启动定时器
        self.buy_timer.start(buy_interval)
        time.sleep(3)
        self.sell_timer.start(sell_interval)

        self.start_button.setDisabled(True)
        self.stop_button.setEnabled(True)

    def schedule_action(self, action, interval):
        while True:
            action()
            time.sleep(interval)  # 这会暂停当前线程，但不会影响其他线程或主线程

    def stop_timers(self):
        if self.executor:
            self.executor.shutdown(wait=False)  # 关闭线程池
            self.executor = None  # 重置 executor 以便下次使用时重新创建

        # 停止定时器
        self.buy_timer.stop()
        self.sell_timer.stop()

        self.start_button.setEnabled(True)
        self.stop_button.setDisabled(True)

        self.auto_action.close_browser()
        self.auto_action = None

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())